package com.cyy;

/**
 * 双向链表
 * @author 会玩的洋洋
 * @since 2022-02-22
 * @param <E>
 */
public class DoubleLinkedList<E> extends AbstractList<E> {
    /**
     * 头结点
     */
    private Node<E> firstNode;

    /**
     * 尾结点
     */
    private Node<E> lastNode;

    @Override
    public void clear() {
        firstNode = null;
        lastNode = null;
        size = 0;
    }

    @Override
    public E get(int index) {
        return node(index).element;
    }

    @Override
    public E set(int index, E element) {
        Node<E> node = node(index);
        E oldElement = node.element;
        node.element = element;
        return oldElement;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckAdd(index);
        if (index == size){
            // 尾部插入
            Node<E> oldNode = lastNode;
            lastNode = new Node<>(oldNode, null, element);
            // 第一个添加的元素
            if (oldNode == null) {
                firstNode = lastNode;
            } else {
                oldNode.next = lastNode;
            }
        } else {
            // 正常情况
            // 获取插入下标的值
            Node<E> node = node(index);
            Node<E> prev = node.prev;
            Node<E> newNode = new Node<>(prev, node, element);
            node.prev = newNode;
            // 第一个插入的节点 (头结点）
            if (prev == null) {
                firstNode = newNode;
            } else {
                // 其他情况
                prev.next = newNode;
            }
        }
        ++size;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> node = node(index);
        if (index == 0) {
            // 删除头结点
            firstNode = firstNode.next;
            firstNode.prev = null;
        } else if (index == size - 1) {
            // 删除尾节点
            lastNode = lastNode.prev;
            lastNode.next = null;
        } else {
            // 正常情况
            Node<E> prev = node.prev;
            prev.next = node.next;
            node.next.prev = prev;
        }
        --size;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = firstNode;
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (node.element == null) {
                    return i;
                }
                node = node.next;
            }
        } else {
            for (int i = 0; i < size; i++) {
                if (node.element.equals(element)) {
                    return i;
                }
                node = node.next;
            }
        }
        return FAILED_RETURN;
    }

    private Node<E> node(int index) {
        rangeCheck(index);

        // 判断下标是否大于一半
        if (index < (size >> 1)) {
            Node<E> node = firstNode;
            for (int i = 0; i < index; i++) {
                node = node.next;
            }
            return node;
        } else {
            Node<E> node = lastNode;
            for (int i = size - 1; i > index; i--) {
                node = node.prev;
            }
            return node;
        }
    }

    private static class Node<E> {
        Node<E> prev;
        Node<E> next;
        E element;

        public Node() {
        }

        public Node(Node<E> prev, Node<E> next, E element) {
            this.prev = prev;
            this.next = next;
            this.element = element;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "element=" + element +
                    '}';
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("DoubleLinkedList{")
                .append("firstNode=")
                .append(firstNode)
                .append(", lastNode=")
                .append(lastNode);
        Node<E> node = firstNode;
        while (node != null) {
            sb.append("{element=").append(node.element).append("}");
            node = node.next;
        }

        sb.append(", size=").append(size).append("}");
        return sb.toString();
    }
}
